package com.tjn.list_;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class ListExercise02 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Book("红楼梦", 100.0, "曹雪芹"));
        list.add(new Book("三国演义", 90.0, "罗贯中"));
        list.add(new Book("水浒传", 80.0, "施耐庵"));

        System.out.println("====排序前====");
        for (Object o : list) {
            System.out.println(o);
        }

        bubble(list);

        System.out.println("====排序后====");
        for (Object o : list) {
            System.out.println(o);
        }
    }


    public static void bubble(List list){
        int temp = 0;
        for (int i = 0; i < list.size(); i++) {
            for(int j = 0 ; j < list.size()-1-i; j++){
                Book book = (Book) list.get(j);
                Book book1 = (Book) list.get(j+1);
                if(book.getPrice() > book1.getPrice()){
                    list.set(j,book1);
                    list.set(j+1,book);
                }
            }
        }
    }
}

class Book{
    private String name;
    private double price;
    private String author;

    public Book(String name, double price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}